package com.app.aipass.fragment.my.safe;

import android.app.Application;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.app.aipass.app.AppApplication;
import com.app.aipass.data.DemoRepository;
import com.app.aipass.entity.UserEntity;
import com.app.aipass.entity.UserInfoEntity;
import com.app.aipass.http.response.ApiException;
import com.app.aipass.utils.ToastUtil;
import com.app.aipass.utils.Utils;

import java.io.IOException;
import java.util.HashMap;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class SafeViewModel extends BaseViewModel<DemoRepository> {

    public SingleLiveEvent<BaseResponse> mBaseResponse = new SingleLiveEvent<>();

    public SafeViewModel(@NonNull Application application, DemoRepository repository) {
        super(application, repository);
    }
    /**
     * 短信验证码
     */
    public void smsCode(TextView send_yzm_tv)
    {
        UserEntity mUserEntity = ((AppApplication)AppApplication.getInstance()).getUserEntity();
        if(mUserEntity == null)
        {
            ToastUtil.showToast("手机号为空");
            return;
        }
        //type:手机验证码类型：1:注册，2:找回密码，3:更换手机号, 4:添加银行卡, 5:设置支付密码,6: 登陆, 11: otc支付, 12: 挂单 用户, 13:挂单 订单
        HashMap<String, Object> map = new HashMap<>();
        map.put("type","5");
        map.put("phone",((AppApplication)AppApplication.getInstance()).getUserEntity().mobile);//挂单数量
//        Log.e("TAG","============smsCode============="+((AppApplication)AppApplication.getInstance()).getUserEntity().mobile);
        model.smsCode(map)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<ResponseBody>() {
                    @Override
                    public void onNext(ResponseBody response) {
                        Log.e("TAG","============smsCode=====onNext========");
                        if(null != response)
                        {
//                            mPlanIndexEntity.setValue(response.getData());
                        }
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","============smsCode=====onError========");
                        if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShortSafe(ApiException.getShowmessage(((ResponseThrowable) throwable).errorBody));
                        }
                    }
                    @Override
                    public void onComplete() {
                        Log.e("TAG","============smsCode=====onComplete========");
                        ToastUtils.showShortSafe("发送成功");
                        send_yzm_tv.setEnabled(false);
//                        send_yzm_tv.setBackgroundResource(R.drawable.shape_d9dade_50);
                        Utils.showCount(send_yzm_tv);
                    }
                });
    }
    public void userInfo() {
        Log.e("TAG","==========requestNetWork======otcConfig=======");
        //可以调用addSubscribe()添加Disposable，请求与View周期同步
        HashMap<String, Object> map = new HashMap<>();
//        lang=zh-cn
        map.put("lang","zh-cn");
        model.userInfo(map)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<UserInfoEntity>>() {
                    @Override
                    public void onNext(BaseResponse<UserInfoEntity> response) {
                        if(response.getCode() == 200)
                        {
                            Log.e("TAG", "=======userInfo================"+response.getData().mobile_hide);
//                            mUserInfoEntity.postValue(response.getData());
                        }
//                        //清除列表
//                        observableList.clear();
//                        //请求成功
//                        if (response.getCode() == 1) {
//                            for (DemoEntity.ItemsEntity entity : response.getResult().getItems()) {
//                                TransactionTabItemViewModel itemViewModel = new TransactionTabItemViewModel(TransactionTabViewModel.this, entity);
//                                //双向绑定动态添加Item
//                                observableList.add(itemViewModel);
//                            }
//                        } else {
//                            //code错误时也可以定义Observable回调到View层去处理
//                            ToastUtils.showShort("数据错误");
//                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","==========requestNetWork======otcConfig===onError====");
                        //关闭对话框
//                        dismissDialog();
//                        //请求刷新完成收回
//                        uc.finishRefreshing.call();
//                        if (throwable instanceof ResponseThrowable) {
//                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
//                        }
                    }

                    @Override
                    public void onComplete() {
                        Log.e("TAG","==========requestNetWork======otcConfig===onComplete====");
                        //关闭对话框
//                        dismissDialog();
                        //请求刷新完成收回
//                        uc.finishRefreshing.call();
                    }
                });
    }

    public void smsSetpassword() {
        //可以调用addSubscribe()添加Disposable，请求与View周期同步
        HashMap<String, Object> map = new HashMap<>();
        map.put("lang","zh-cn");//account_type=mobile&type=payment
        map.put("account_type","mobile");
        map.put("type","payment");
        model.smsSetpassword(map)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse response) {

//                        dismissDialog();
                        //请求成功
                        if (response.getCode() == 200) {
//                            mUpdateCongigEntity.postValue(response.getData());

//                            setPassword();
                        } else {
                            //code错误时也可以定义Observable回调到View层去处理
                            ToastUtils.showShort("数据错误");
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        //关闭对话框
                        dismissDialog();
                        if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        //关闭对话框
                        dismissDialog();
                    }
                });
    }
    public void reSetUserPassword(HashMap<String, Object> map) {
        model.reSetUserPassword(map)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<Response<Void>>() {
                    @Override
                    public void onNext(Response<Void> response) {
                        try {
                            Log.e("TAG","============reSetUserPassword=====onNext========");

                            if(response != null)
                            {
                                if(response.code() >= 200 && response.code() < 300)
                                {
                                    //关闭对话框
//                                    dismissDialog();
                                    ToastUtils.showShort("设置成功");
                                    finish();
                                }
                                else
                                {
//                                    dismissDialog();
                                    ToastUtils.showShortSafe(ApiException.getShowmessage(response.errorBody().string()));
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(Throwable throwable) {
//                        Log.e("TAG","============reSetUserPassword=====onError========"+throwable.getMessage());
//                        //关闭对话框
//                        dismissDialog();
//                        if (throwable instanceof ResponseThrowable) {
////                            Log.e("TAG",((ResponseThrowable) throwable).code + "============reSetUserPassword=====onError========"+((ResponseThrowable) throwable).errorBody);
//                            ToastUtils.showShortSafe(ApiException.getShowmessage(((ResponseThrowable) throwable).errorBody));
//                        }
                    }

                    @Override
                    public void onComplete() {
//                        Log.e("TAG","============reSetUserPassword=====onComplete========");
//                        //关闭对话框
//                        dismissDialog();
//                        ToastUtils.showShort("设置成功");
//                        finish();
                    }
                });
    }
//    public void setPassword2() {
//        //可以调用addSubscribe()添加Disposable，请求与View周期同步
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("lang","zh-cn");
//        //password=123456&confirm_password=123456&type=payment
//        // &mobile_code=953102&email_code=&ga_code=
//        map.put("password","123456");
//        map.put("confirm_password","123456");
//        map.put("type","payment");
//        map.put("mobile_code","953102");
//        map.put("email_code","");
//        map.put("ga_code","");
//        model.setPassword(map)
//                .compose(RxUtils.schedulersTransformer()) //线程调度
//                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
//                .doOnSubscribe(this)//请求与ViewModel周期同步
//                .doOnSubscribe(new Consumer<Disposable>() {
//                    @Override
//                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
//                    }
//                })
//                .subscribe(new DisposableObserver<BaseResponse>() {
//                    @Override
//                    public void onNext(BaseResponse response) {
//
////                        dismissDialog();
//                        //请求成功
//                        if (response.getCode() == 200) {
////                            mUpdateCongigEntity.postValue(response.getData());
//                        } else {
//                            //code错误时也可以定义Observable回调到View层去处理
//                            ToastUtils.showShort("数据错误");
//                        }
//                    }
//
//                    @Override
//                    public void onError(Throwable throwable) {
//                        //关闭对话框
//                        dismissDialog();
//                        if (throwable instanceof ResponseThrowable) {
//                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
//                        }
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        //关闭对话框
//                        dismissDialog();
//                    }
//                });
//    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}